package server;

import database.BeePool;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;
import tools.FileoutputUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.Map.Entry;

public class CashItemFactory {
    private static CashItemFactory instance;
    private static int[] bestItems;
    private boolean initialized;
    private Map<Integer, List<Integer>> openBox;
    private Map<Integer, CashItemInfo> itemStats;
    private Map<Integer, List<CashItemInfo>> itemPackage;
    private Map<Integer, CashItemInfo.CashModInfo> itemMods;
    private Map<Integer, Integer> itemIdToSN;
    private Map<Integer, Integer> itemIdToSn;
    private MapleDataProvider data;
    private Map<Integer, Integer> idLookup;

    public static CashItemFactory getInstance() {
        return CashItemFactory.instance;
    }

    protected CashItemFactory() {
        this.initialized = false;
        this.openBox = (Map<Integer, List<Integer>>) new HashMap();
        this.itemStats = (Map<Integer, CashItemInfo>) new HashMap();
        this.itemPackage = (Map<Integer, List<CashItemInfo>>) new HashMap();
        this.itemMods = (Map<Integer, CashItemInfo.CashModInfo>) new HashMap();
        this.itemIdToSN = (Map<Integer, Integer>) new HashMap();
        this.itemIdToSn = (Map<Integer, Integer>) new HashMap();
        this.data = MapleDataProviderFactory.getDataProvider("Etc.wz");
        this.idLookup = (Map<Integer, Integer>) new HashMap();
    }

    public void initialize() {
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:初始化购物商城物品");
        this.itemPackage.clear();
        this.itemMods.clear();
        this.refreshAllModInfo();
    }

    public int getSnByItemItd(int itemid) {
        int sn = ((Integer) this.itemIdToSN.get(Integer.valueOf(itemid))).intValue();
        return sn;
    }

    public int getSnByItemItd2(int itemid) {
        int sn = ((Integer) this.itemIdToSn.get(Integer.valueOf(itemid))).intValue();
        return sn;
    }

    public CashItemInfo getItem(int sn) {
        CashItemInfo stats = (CashItemInfo) this.itemStats.get(Integer.valueOf(sn));
        CashItemInfo.CashModInfo z = this.getModInfo(sn);
        if (z != null && z.showUp) {
            return z.toCItem(stats);
        }
        if (stats == null || !stats.onSale()) {
            return null;
        }
        return stats;
    }

    public Set<Integer> getAllItemSNs() {
        return this.itemStats.keySet();
    }

    public List<CashItemInfo> getAllItems() {
        return new ArrayList(this.itemStats.values());
    }

    public List<CashItemInfo> getPackageItems(int itemId) {
        if (this.itemPackage.get(Integer.valueOf(itemId)) != null) {
            return this.itemPackage.get(Integer.valueOf(itemId));
        }
        List<CashItemInfo> packageItems = (List<CashItemInfo>) new ArrayList();
        MapleData b = this.data.getData("CashPackage.img");
        if (b == null || b.getChildByPath(itemId + "/SN") == null) {
            return null;
        }
        for (MapleData d : b.getChildByPath(itemId + "/SN").getChildren()) {
            packageItems.add(this.itemStats.get(Integer.valueOf(MapleDataTool.getIntConvert(d))));
        }
        this.itemPackage.put(Integer.valueOf(itemId), packageItems);
        return packageItems;
    }

    public Map<Integer, List<Integer>> getRandomItemInfo() {
        return this.openBox;
    }

    public boolean getModInfoSN(int sn) {
        return this.itemMods.get(Integer.valueOf(sn)) != null;
    }

    public CashItemInfo.CashModInfo getModInfo(int sn) {
        CashItemInfo.CashModInfo ret = (CashItemInfo.CashModInfo) this.itemMods.get(Integer.valueOf(sn));
        PreparedStatement ps = null;
        ResultSet rs = null;
        if (ret == null) {
            if (this.initialized) {
                return null;
            }
            try (Connection con = BeePool.getConnection()) {
                ps = con.prepareStatement("SELECT * FROM cashshop_modified_items WHERE serial = ?");
                ps.setInt(1, sn);
                rs = ps.executeQuery();
                if (rs.next()) {
                    ret = new CashItemInfo.CashModInfo(sn, rs.getInt("discount_price"), rs.getInt("mark"), rs.getInt("showup") > 0, rs.getInt("itemid"), rs.getInt("priority"), rs.getInt("package") > 0, rs.getInt("period"), rs.getInt("gender"), rs.getInt("count"), rs.getInt("meso"), rs.getInt("unk_1"), rs.getInt("unk_2"), rs.getInt("unk_3"), rs.getInt("extra_flags"), rs.getInt("type"));
                    this.itemMods.put(Integer.valueOf(sn), ret);
                }
            } catch (Exception e) {
                FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
                e.printStackTrace();
            }
        }
        return ret;
    }

    private void refreshAllModInfo() {
        this.itemMods.clear();
        this.itemIdToSn.clear();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try (Connection con = BeePool.getConnection()) {
            ps = con.prepareStatement("SELECT * FROM cashshop_modified_items");
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer sn = Integer.valueOf(rs.getInt("serial"));
                CashItemInfo.CashModInfo ret = new CashItemInfo.CashModInfo(sn.intValue(), rs.getInt("discount_price"), rs.getInt("mark"), rs.getInt("showup") > 0, rs.getInt("itemid"), rs.getInt("priority"), rs.getInt("package") > 0, rs.getInt("period"), rs.getInt("gender"), rs.getInt("count"), rs.getInt("meso"), rs.getInt("unk_1"), rs.getInt("unk_2"), rs.getInt("unk_3"), rs.getInt("extra_flags"), rs.getInt("type"));
                this.itemMods.put(sn, ret);
                this.itemIdToSn.put(Integer.valueOf(ret.itemid), sn);
            }
        } catch (Exception e) {
            FileoutputUtil.outError("logs/资料库异常.txt", (Throwable) e);
            e.printStackTrace();
        }
    }

    public Collection<CashItemInfo.CashModInfo> getAllModInfo() {
        if (this.itemMods.isEmpty()) {
            this.refreshAllModInfo();
        }
        return this.itemMods.values();
    }

    public int[] getBestItems() {
        return CashItemFactory.bestItems;
    }

    public void clearItems() {
        this.refreshAllModInfo();
    }

    public int getItemSN(int itemid) {
        for (Entry<Integer, CashItemInfo> ci : this.itemStats.entrySet()) {
            if (((CashItemInfo) ci.getValue()).getId() == itemid) {
                return ((CashItemInfo) ci.getValue()).getSN();
            }
        }
        return 0;
    }

    public void clearCashShop() {
        this.itemStats.clear();
        this.itemPackage.clear();
        this.itemMods.clear();
        this.idLookup.clear();
        this.initialized = false;
        this.initialize();
    }

    static {
        instance = new CashItemFactory();
        bestItems = new int[]{50100010, 50100010, 50100010, 50100010, 50100010};
    }
}
